<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 */

defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'rank';
//exi('xxxx','error');
$return=array();
//查询当前粉丝是否关注当前公众号
$fans = cfc('core')->get_fans($_SESSION['openid']);
$fansinfo = pdo_get('core_fans',array('id'=>$fans['id'],'uniacid'=>$_SESSION['uniacid']));
$follow = $fansinfo['follow'];
set_html('title','排行榜');
if($_RQ['op']=='main'){
	if($_RQ['id']){
		$id=intval($_RQ['id']);
	}
	$item=pdo_get('jy_h5game_game',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('name'));
	$return['html']='../'.ADDONS.'/jy_h5game/custom/templates/mobile/2048/index.html';
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='rank'){		
	if($_RQ['rid']){
		$id=intval($_RQ['rid']);
	}
	 //查询对应活动的参数
	$item=pdo_get('jy_h5game_reply',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	//获取当前时间
	$time = time();	
	//是否关注公众号
	if($follow!= 1){
		$return['follow']=$follow;
		//exi('请先关注公众号','error');
	}
	//实际参与该活动的人数
	 $playerNum = pdo_count('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	 //总人数=实际人数+虚拟人数
	 $playerNums = $playerNum + $item['virtualRegNum'];
	 
	 //活动奖项数据 转成数组格式
	 $award = json_decode($item['award'],true);
	 //活动奖项数量
	 $awardNum = count($award);
	 
	//奖品总数量
	for ($x=0; $x<$awardNum; $x++) {
		$allPrize += $award[$x]['prizeNum'];
	} 

   //按排名取与奖品数量等同的玩家(被拉黑玩家不在计量范围)	
   if($allPrize){
		$list = pdo_getall('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'shielding'=>!2),'','','bestScore desc limit 0,'.$allPrize);
   }
	//排行榜
	if($item['isRank'] == 2 && $id){
		$rankList = pdo_getall('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'shielding'=>!2),'','','bestScore desc limit 0,'.$item['rank']);
	}
	 //该活动玩家积分排名(前30)
	 elseif($id){
		$rankList = pdo_getall('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'shielding'=>!2),'','','bestScore desc limit 0,30');
	 }else{
		$rankList = pdo_getall('jy_h5game_fans',array('uniacid'=>$_SESSION['uniacid'],'shielding'=>!2),'','','bestScore desc limit 0,30');
	 }
	 //每个奖项奖品数量
	for ($x=0; $x<$awardNum; $x++) {
		$prize[$x] = $award[$x]['prizeNum'];
    } 
	if($list){
		 $paiming = 1;
		foreach($list as &$row){
			$row['paiming']=$paiming;
			$paiming = $paiming+1;
		}
	}
	if($rankList){
		 $paiming = 1;
		foreach($rankList as &$row){
			if($row['fansID']==$fansinfo['mid']){
				$return['paiming']=$paiming;
			}
			$row['paiming']=$paiming;
			$paiming = $paiming+1;
		}
	}
    //当前玩家信息
	$play = pdo_get('jy_h5game_fans',array('fansID'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid']));
	//当前玩家得分
	$score = pdo_getcolumn('jy_h5game_fans',array('fansID'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$id),'bestScore');
	$return['award'] = array('action'=>'award','query'=>'id='.$id,'plugin'=>'jy_h5game');
	$return['notice'] = array('action'=>'notice','query'=>'id='.$id,'plugin'=>'jy_h5game');
	$return['info'] = array('action'=>'info','query'=>'id='.$id,'plugin'=>'jy_h5game');
	$return['code'] = $errorCode;
	$return['msg'] = '2222222222';
    $return['data'] = $award;
	//游戏结束后按排名将玩家存入获奖名单
	if($item['status']==3&&$item['gameTypeSetting']==3){
		if($list){
			foreach($list as &$row){
				//奖项1
				if($row['paiming']<=$prize['0']){			
						$insert=array( 
						'rid'=>$row['rid'],
						'fansID'=>$row['fansID'],
						'awardinfo'=>$row['name'],
						'createtime'=>TIMESTAMP,
						'awardLevel'=>'1',
						'award'=>$award['0']['prizeName'],
						'uniacid'=>$_SESSION['uniacid'],
						'ausername'=>$row['ausername'],
						'address'=>$row['address'],
						'aphone'=>$row['tel'],
						'awardTime'=>time()
					);
				}
				//奖项2
				if($row['paiming']>$prize['0']&&$row['paiming']<=($prize['0']+$prize['1'])){
							
					$insert=array( 
						'rid'=>$row['rid'],
						'fansID'=>$row['fansID'],
						'awardinfo'=>$row['name'],
						'createtime'=>TIMESTAMP,
						'awardLevel'=>'2',
						'award'=>$award['1']['prizeName'],
						'uniacid'=>$_SESSION['uniacid'],
						'ausername'=>$row['ausername'],
						'address'=>$row['address'],
						'aphone'=>$row['tel'],
						'awardTime'=>time()
						
					);
				}
				//奖项3
				if($row['paiming']>($prize['0']+$prize['1'])&&$row['paiming']<=($prize['0']+$prize['1']+$prize['2'])){				
					$insert=array( 
						'rid'=>$row['rid'],
						'fansID'=>$row['fansID'],
						'awardinfo'=>$row['name'],
						'createtime'=>TIMESTAMP,
						'awardLevel'=>'3',
						'award'=>$award['2']['prizeName'],
						'uniacid'=>$_SESSION['uniacid'],
						'ausername'=>$row['ausername'],
						'address'=>$row['address'],
						'aphone'=>$row['tel'],
						'awardTime'=>time()
						
					);
				}
				//奖项4
				if($row['paiming']>($prize['0']+$prize['1']+$prize['2'])&&$row['paiming']<=($prize['0']+$prize['1']+$prize['2']+$prize['3'])){				
					$insert=array( 
						'rid'=>$row['rid'],
						'fansID'=>$row['fansID'],
						'awardinfo'=>$row['name'],
						'createtime'=>TIMESTAMP,
						'awardLevel'=>'4',
						'award'=>$award['3']['prizeName'],
						'uniacid'=>$_SESSION['uniacid'],
						'ausername'=>$row['ausername'],
						'address'=>$row['address'],
						'aphone'=>$row['tel'],
						'awardTime'=>time()			
					);
				}
				 $check = pdo_getcolumn('jy_h5game_award',array('rid'=>$row['rid'],'uniacid'=>$_SESSION['uniacid'],'fansID'=>$row['fansID']),'id');
				if(!$check){
					$this->insert('award',$insert);
				}
			}
		}
	}
	$return['item']=$item;
	$return['play']=$play;
	$return['fansinfo']=$fansinfo;
	$return['playerNums']=$playerNums;
	$return['list']=$list;
	$return['score']=$score;
	$return['rankList']=$rankList;
	exi($return);
}
